package com.pickgliss.utils
{
	import com.pickgliss.events.InteractiveEvent;
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.ui.controls.ISelectable;
	import com.pickgliss.ui.controls.cell.IListCell;
	
	import flash.display.InteractiveObject;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * 
	 * @author James
	 * 
	 */
	public final class DoubleClickManager
	{
		private static var _instance:DoubleClickManager;
		/**
		 * 
		 * 获得此类的实例
		 * 
		 * @return 
		 * 
		 */		
		public static function get Instance():DoubleClickManager
		{
			if(!_instance)
				_instance = new DoubleClickManager();
			return _instance;
		}
		
		private const DoubleClickSpeed:uint = 350;
		
		private var _timer:Timer;
		private var _currentTarget:InteractiveObject;
		private var _ctrlKey:Boolean;
		
		private var _isClicking:Boolean;
		public function get isClicking():Boolean
		{
			return _isClicking;
		}
		public function set isClicking(value:Boolean):void
		{
			_isClicking = value;
		}
		
		
		public function DoubleClickManager(){
			init();
		}
		
		/**
		 * 
		 * 开启组件双击，需要监听 InteractiveEvent.CLICK 和 InteractiveEvent.DOUBLE_CLICK 事件
		 * 
		 * @param target 需要监听双击事件的对象
		 * 
		 */		
		public function enableDoubleClick(target:InteractiveObject):void{
			if(target)
				target.addEventListener(MouseEvent.MOUSE_DOWN, __mouseDownHandler);
		}
		
		/**
		 * 
		 * 关闭组件双击，需要移除 InteractiveEvent.CLICK 和 InteractiveEvent.DOUBLE_CLICK 事件
		 * 
		 * @param target 需要移除双击事件的对象
		 * 
		 */	
		public function disableDoubleClick(target:InteractiveObject):void{
			if(target)
				target.removeEventListener(MouseEvent.MOUSE_DOWN, __mouseDownHandler);
		}
		
		private function init():void{
			_timer = new Timer(DoubleClickSpeed, 1);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, __timerCompleteHandler);
		}
		
		private function getEvent(type:String):InteractiveEvent{
			var interactiveEvent:InteractiveEvent = new InteractiveEvent(type);
			interactiveEvent.ctrlKey = _ctrlKey;
			return interactiveEvent;
		}
		
		private function __timerCompleteHandler(evt:TimerEvent):void{
			
			trace("click==================");
			StageReferance.stage.removeEventListener(MouseEvent.CLICK,__stageClickHandler,true);
			isClicking = false;
			_currentTarget.dispatchEvent(getEvent(InteractiveEvent.CLICK));
			if(_currentTarget is IListCell || _currentTarget is ISelectable)
				_currentTarget.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
		
		private function __mouseDownHandler(evt:MouseEvent):void{
			_ctrlKey = evt.ctrlKey;
			if(_timer.running)
			{
				_timer.stop();
				isClicking = false;
				StageReferance.stage.removeEventListener(MouseEvent.CLICK,__stageClickHandler,true);
				if(_currentTarget != evt.currentTarget){
					return;
				}
				evt.stopImmediatePropagation();
				_currentTarget.dispatchEvent(getEvent(InteractiveEvent.DOUBLE_CLICK));
				trace("doubleclick==================");
			}
			else
			{
				StageReferance.stage.addEventListener(MouseEvent.CLICK,__stageClickHandler,true,99);
				_timer.reset();
				_timer.start();
				_currentTarget = evt.currentTarget as InteractiveObject;
				isClicking = true;
			}
		}
		
		private function __stageClickHandler(e:MouseEvent):void
		{
			if(e.target != this)
			{
				e.stopImmediatePropagation();
				e.stopPropagation();
			}
		}
	}
}